<?php
/**
 * Created by PhpStorm.
 * User: SongPeiLin
 * Date: 2018/10/15
 * Time: 16:51
 */

namespace app\admin\controller;


use app\admin\model\UserRoleCate;
use app\admin\model\UserRoleCateModel;
use app\portal\model\PortalCategoryModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;
use tree\Tree;

class CategorySetController extends AdminBaseController
{
    /**
     * 所有的后台用户列表
     */
    public function user_list()
    {
        $content = hook_one('user_admin_index_view');

        if (!empty($content)) {
            return $content;
        }

        $where = [];
        $request = input('request.');

        if (!empty($request['uid'])) {
            $where['id'] = intval($request['uid']);
        }
        $keywordComplex = [];
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $keywordComplex['user_login|user_nickname|user_email|mobile'] = ['like', "%$keyword%"];
        }
        $usersQuery = Db::name('user');

        $list = $usersQuery->whereOr($keywordComplex)->where($where)->order("create_time DESC")->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 所有的分类列表
     */
    public function all_category()
    {
           $uid =input('get.uid');
        if($uid){
            $modwhere['rolo_user_id']=$uid;
        }else{
            $this->error('错误的用户');
        }
            $content = hook_one('portal_admin_category_index_view');

            if (!empty($content)) {
                return $content;
            }


            $portalCategoryModel = new PortalCategoryModel();


            $where = ['delete_time' => 0];

            $categories = $portalCategoryModel->order("list_order ASC")->where($where)->select()->toArray();

            $tree = new Tree();
            $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
            $tree->nbsp = '&nbsp;&nbsp;';
            //查询所拥有文章分类权限，数组;如果uid 是1，不能修改；
            $catemod=new UserRoleCateModel();
            $currentIds = $catemod->getcate($modwhere);
            if(!$currentIds){
                $currentIds =0;
            }
            if (!is_array($currentIds)) {
                $currentIds = [$currentIds];
            }

            $newCategories = [];

            foreach ($categories as $item) {

                $item['parent_id_node'] = ($item['parent_id']) ? ' class="child-of-node-' . $item['parent_id'] . '"' : '';
                $item['style'] = empty($item['parent_id']) ? '' : 'display:none;';
                $item['checked'] = in_array($item['id'], $currentIds) ? "checked" : "";
                $item['url'] = cmf_url('portal/List/index', ['id' => $item['id']]);

                array_push($newCategories, $item);
            }

            $tree->init($newCategories);

            if (empty($tpl)) {
                $tpl = "
                        <td style='padding-left:20px;'><input type='checkbox' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids[]' value='\$id' \$checked data-parent_id='\$parent_id' data-id='\$id' onclick='javascript:checknode(this);'></td>
                        <td>\$id</td>
                        <td>\$spacer <a href='\$url' target='_blank'>\$name</a></td>

                    </tr>";
            }
            $treeStr = $tree->getTree(0, $tpl);

            $this->assign('category_tree', $treeStr);
            $this->assign('uid', $uid);



            $keyword = null;
            $this->assign('keyword', $keyword);

            return $this->fetch();
        }
    /**
     * 获取授权的权限
     */
    public function get_rule(){

        $ids =Request::instance()->has('ids','post');
        $uid =Request::instance()->has('uid','post');
         if($ids&&$uid){
             $data =input('');
             $idsarr=[];
             $idsarr=array_merge($idsarr,$data['ids']);
             $where['rolo_user_id'] =$data['uid'];
             $usecatemod=new UserRoleCateModel();
             foreach($idsarr as $key=>$value){
                 $where['rolo_category'] =$value;
                 $usecatemod->insert($where);
             }
             $this->success('设置成功');
         }else{
            $this->error('参数不存在');
         }



    }


}
